/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package twittertransform;

/**
 *
 * @author Menyueru
 */

import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import twitter4j.*;
import twitter4j.conf.ConfigurationBuilder;


public class Twittertransform 
{
    static List<String> hashtags;
    static List<Long> users;
    static Map<Long,Map<String,Integer>> userhashtags;
    static Map<Long,Map<Long,Integer>> userretweet;
    private static Twitter configureTwitter() 
    {
        ConfigurationBuilder configurationBuilder = new ConfigurationBuilder();
        configurationBuilder.setDebugEnabled(true)
                .setOAuthConsumerKey("JuJEiHyjL4kEBLA5X4i1w")
                .setOAuthConsumerSecret("reyGgphzgqxQ1yccsHjpX7qzoGlTBFikj7odaomWM")
                .setOAuthAccessToken("124344112-Dl4RiVj1s1p3v9MHggqgn7O1OJrKXmGsbCcaJJ8e")
                .setOAuthAccessTokenSecret("2UZhjixc6Z5R0FjCDXmom5FL75vJZUv9CGGfdGk");
        TwitterFactory tf = new TwitterFactory(configurationBuilder.build());
        return tf.getInstance();
    }
    
    public static void main(String[] args)
    {
        getTweets();
        try 
        {
            File file = new File(System.getProperty("user.dir")+"/hashtags.arff");
            File file2 = new File(System.getProperty("user.dir")+"/retweets.arff");
            if (!file.exists()) 
            {
		file.createNewFile();
            }
            if (!file2.exists()) 
            {
		file2.createNewFile();
            }
            FileWriter fw = new FileWriter(file.getAbsoluteFile());
            BufferedWriter bw = new BufferedWriter(fw);
            FileWriter fw2 = new FileWriter(file2.getAbsoluteFile());
            BufferedWriter bw2 = new BufferedWriter(fw2);
            bw.write("@relation 'hashtags'");
            bw.newLine();
            bw.newLine();
            bw.write("@attribute 'id' numeric");
            bw.newLine();
            for(String n : hashtags)
            {
               bw.write("@attribute "+n+" numeric");
               bw.newLine();
            }
            bw.newLine();
            bw.write("@data");
            bw.newLine();
            bw.newLine();
            bw2.write("@relation 'retweets'");
            bw2.newLine();
            bw2.newLine();
            bw2.write("@attribute 'id' numeric");
            bw2.newLine();
            for(Long n : users)
            {
               bw2.write("@attribute "+n.toString()+" numeric");
               bw2.newLine();
            }
            bw2.newLine();
            bw2.write("@data");
            bw2.newLine();
            bw2.newLine();
            
            for(Long n : userhashtags.keySet())
            {
                if(userhashtags.get(n).size()>0)
                {
                    bw.write(n+".0");
                    for(String t : hashtags)
                    {
                        if(userhashtags.get(n).containsKey(t))
                        {
                            bw.write(","+userhashtags.get(n).get(t));
                        }
                        else
                        {
                            bw.write(",0");
                        }
                    }
                     bw.newLine();
                }
            } 
            
            for(Long n : userretweet.keySet())
            {
                if(userretweet.get(n).size()>0)
                {
                    bw2.write(n+".0");
                    for(Long t : users)
                    {
                        if(userretweet.get(n).containsKey(t))
                        {
                            bw2.write(","+userretweet.get(n).get(t));
                        }
                        else
                        {
                            bw2.write(",0");
                        }
                    }
                     bw2.newLine();
                }
            }  
            bw.close();
            bw2.close();
        }catch (IOException e) 
        {
		e.printStackTrace();
	}
    }
    
    
    public static void getTweets () 
    {
        
        hashtags=new LinkedList<String>();
        users=new LinkedList<Long>();
        userhashtags=new HashMap<Long,Map<String,Integer>>();
        userretweet=new HashMap<Long,Map<Long,Integer>>();
        Twitter twitter = configureTwitter();
        try {
            Query query = new Query();
            query.setCount(10);
            query.setGeoCode(new GeoLocation(18.98422d, -69.938965d), 180.0d, Query.KILOMETERS);
            query.setLang("es");
            QueryResult result = twitter.search(query);
            int count=0;
            do {
                for (Status tweet : result.getTweets()) 
                {
                    Map temph, tempr;
                    Long id= tweet.getUser().getId();
                    if(userhashtags.containsKey(id))
                    {
                        temph=userhashtags.get(id);
                        tempr=userretweet.get(id);
                    }
                    else 
                    {
                        temph= new HashMap<String,Integer>();
                        tempr= new HashMap<Long,Integer>();
                    }
                    
                    HashtagEntity[] tags= tweet.getHashtagEntities();
                    for(HashtagEntity h : tags)
                    {
                        if(!hashtags.contains(h.getText()))
                            hashtags.add(h.getText());
                        if(!temph.containsKey(h.getText()))
                            temph.put(h.getText(), 1);
                        else
                            temph.put(h.getText(),(Integer)temph.get(h.getText()) +1);
                    }
                    Status S=tweet.getRetweetedStatus();
                    if(S!=null)
                    {
                        if(!users.contains(S.getUser().getId()))
                            users.add(S.getUser().getId());
                        if(!tempr.containsKey(S.getUser().getId()))
                            tempr.put(S.getUser().getId(), 1);
                        else
                            tempr.put(S.getUser().getId(),(Integer)tempr.get(S.getUser().getId()) +1);
                    }
                    userretweet.put(id, tempr);
                    userhashtags.put(id, temph);
                    if((++count)>=10)
                        break;
                }
                if((count)>=10)
                        break;
                query = result.nextQuery();
                result = twitter.search(query);
            } while (result.hasNext());
            twitter.shutdown();
        } catch (TwitterException ex) {
            Logger.getLogger(Twittertransform.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}